<?php
class OrderController extends AdminModuleController {
	
	public function actionView() {
		$orderId = getgpc(2);
		$order = $this->OrderModel->findByPk($orderId);

		// 格式化信息
		$logistic = $this->LogisticModel->findByPk($order['logisticId']);
		$order['totalPrice'] = number_format($order['totalPrice'], 2, '.', '');
		$order['logistic'] = $logistic['title'];
		$order['status'] = $this->get_order_status($order);
		$order['payType'] = Lookup::item('payType', $order['payType']);
		$order['createdTime'] = date('Y-m-d H:i:s', $order['createdTime']);
		
		// 读取购物车信息
		/*
		$shoppingcartList = $this->ShoppingcartModel->findAll("orderId='{$order['orderId']}'");
		$totalPrice = 0;	// 产品价格总计
		foreach($shoppingcartList as &$v) {
			$product = $this->ProductModel->findByPk($v['productId']);
			
			if($v['ispromotions']==0) {
				$userPrice = $v['ourPrice']*$v['discount'];
				$v['userPrice'] = $userPrice;//会员价格
				$promotions = '';
			} else {
				$v['userPrice'] = $v['promotionsPrice'];
				$userPrice = $v['promotionsPrice'];
				$promotions = '<font color=red>【促销】</font>';
			}
			
			$v['promotions'] = $promotions;
			$v['content'] = $product['content'];
			$v['productNo'] = $product['productNo'];
			$v['categoryId'] = $product['categoryId'];
			$v['subtotal'] = $userPrice*$v['cartCount'];
			$totalPrice = $totalPrice+$v['subtotal'];
		}
		*/
				
		// 获取收货信息
		
		$consignee = $this->ConsigneeModel->findByPk($order['consigneeId']);
		//print_r($consignee);
		$consignee['area'] = $this->AreaModel->getArea($consignee['city']);
		
		// 获取发票信息
		$invoice = $this->InvoiceModel->findAll(" orderId='".$order['orderNo']."' ");
		//print_r($invoice);
		
		// 显示操作按钮
		$bottons = $this->get_status_button($order);
		
		// 读取操作记录
		$operationList = $this->OperationModel->findAll("module='".GODHOUSE_OPERATION_MODULE_ORDER."' AND pkId='{$orderId}'", 0, 0, "operationId DESC");
		foreach ($operationList as &$v) {
			$v['createdTime'] = date('Y-m-d H:i:s', $v['createdTime']);
		}
		
		$this->view->assign('order', $order);
		//$this->view->assign('totalPrice', $totalPrice);
		$this->view->assign('invoice', $invoice);
		$this->view->assign('consignee', $consignee);
		$this->view->assign('operationList', $operationList);
		$this->view->assign('shoppingcartList', $this->_totalPrice("orderId='{$order['orderId']}'"));
		$this->view->assign('bottons', $bottons);
				
		$this->display('order_view');
		
	}
	
	public function actionAdmin() {
		$page = max(1, getgpc('page'));
		$submitTo = getgpc('submitTo', 'R');
		$toUrl = 'order-admin.htm?';
		$sqladd = '';
		$orderSattus = getgpc('orderStatus', 'R');
		if(is_numeric($orderSattus)) {
			$sqladd .= " orderStatus='$orderSattus' AND";
			$toUrl .= "orderStatus=$orderSattus&";
		}
		$payStatus = getgpc('payStatus', 'R');
		if(is_numeric($payStatus)) {
			$sqladd .= " payStatus='$payStatus' AND";
			$toUrl .= "payStatus=$payStatus&";
		}
		$deliveryStatus = getgpc('deliveryStatus', 'R');
		if(is_numeric($deliveryStatus)) {
			$sqladd .= " deliveryStatus='$deliveryStatus' AND";
			$toUrl .= "deliveryStatus=$deliveryStatus&";
		}
		$userName = getgpc('userName', 'R');
		if($userName) {
			$sqladd .= " userName like '%$userName%' AND";
			$toUrl .= "userName=$userName&";
		}
		$orderNo = getgpc('orderNo', 'R');
		if($orderNo) {
			$sqladd .= " orderNo like '%$orderNo%' AND";
			$toUrl .= "orderNo=$orderNo&";
		}
		$startDate =  getgpc('startDate', 'R');
		$endDate =  getgpc('endDate', 'R');
		if($startDate && $endDate) {
			$startDate2 = strtotime($startDate);
			$endDate2 = strtotime($endDate)+86400;
			$sqladd .= " createdTime>=$startDate2 AND createdTime<=$endDate2 AND";
			$toUrl .= "submitTo=$submitTo&startDate=$startDate&endDate=$endDate&";
		}
		
		/* 内部条件 */
		if($this->groupId !=  GODHOUSE_ADMIN_SYSTEM && $this->groupId !=  GODHOUSE_ADMIN_ADVANCED && $this->groupId !=  GODHOUSE_ADMIN_NORMAL) {
			$sqladd .= " adminId='{$this->userId}' AND";
		}
		
		$sqladd = substr($sqladd, 0, -3);
		$toUrl = substr($toUrl, 0, -1);
		
		$orders = $this->OrderModel->findAll($sqladd, $page, GODHOUSE_PPP3);
		
		foreach($orders as &$v) {
			$v['status'] = $this->get_order_status($v);
			$v['createdTime'] = date('Y-m-d H:i:s', $v['createdTime']);
			$v['totalPrice'] = number_format($v['totalPrice'], 2, '.', '');
			// 是否显示删除按钮
			$v['isDelete'] = 1;
			if($v['orderStatus'] == GODHOUSE_STATUS_ORDER_COMPLETE || $v['orderStatus'] == GODHOUSE_STATUS_ORDER_CONFIRMED) {
				$v['isDelete'] = 0;
			}
			
			//到货日期提醒
			$v['arr_date'] = $this->ShoppingcartModel->count("orderId='$v[orderId]' AND arr_date = '' ");
			//发货日期提醒
			$v['del_date'] = $this->ShoppingcartModel->count("orderId='$v[orderId]' AND del_date = '' ");
		
			
			
		}
		//print_r($orders);
		$num = $this->OrderModel->count($sqladd);
		$multipage = page($num, GODHOUSE_PPP3, $page, $toUrl);
		
		$this->view->assign('userName', $userName);
		$this->view->assign('orderNo', $orderNo);
		$this->view->assign('startDate', $startDate);
		$this->view->assign('endDate', $endDate);
		
		$this->view->assign('multipage', $multipage);
		$this->view->assign('orders', $orders);

		$this->display('order_admin');
	}
	
	public function actionSearch() {
		$orderStatusList = Lookup::items('orderStatus');
		$payStatusList = Lookup::items('payStatus');
		$deliveryStatusList = Lookup::items('deliveryStatus');
		
		$this->view->assign('orderStatusList', $orderStatusList);
		$this->view->assign('payStatusList', $payStatusList);
		$this->view->assign('deliveryStatusList', $deliveryStatusList);
		$this->display('order_search');
	}
	
	public function actionDelete() {
		$orderId = getgpc(2);
		$this->OrderModel->updateByPk($orderId, array(
				'orderStatus'=>-1,
		));
		$this->message('删除成功。', GODHOUSE_DOMAIN_WWW.'admin/order-admin.htm');
	}
	
	public function actionTrueDelete() {
		$orderId = getgpc(2);
		$order = $this->OrderModel->findByPk($orderId);
		if(!$order) {
			$this->message('订单不存在。', GODHOUSE_DOMAIN_WWW.'admin/order-admin.htm');
		}
		if ($order['orderStatus'] == GODHOUSE_STATUS_ORDER_CONFIRMED || $order['orderStatus'] == GODHOUSE_STATUS_ORDER_COMPLETE) {
			$this->message('已确认和已完成的订单不能被删除。', GODHOUSE_DOMAIN_WWW.'admin/order-admin.htm');
		}
		$this->OrderModel->deleteByPk($orderId);
		$this->OperationModel->delete("module='".GODHOUSE_OPERATION_MODULE_ORDER."' AND pkId='$orderId'");
		$this->ConsigneeModel->deleteByPk($order['consigneeId']);
		$this->ShoppingcartModel->delete("orderId='$orderId'");
		$this->message('删除成功。', GODHOUSE_DOMAIN_WWW.'admin/order-admin.htm');
	}
	
	/* 编辑收货人信息 */
	public function actionEditConsignee() {
		$consigneeId = getgpc(2);
		$order = $this->OrderModel->find("consigneeId='$consigneeId'");
		if($order['orderStatus'] == GODHOUSE_STATUS_ORDER_COMPLETE) {
			$this->message('此订单已完成交易，不能编辑收货人信息。');
		}
		$consignee = $this->ConsigneeModel->findByPk($consigneeId);
		// 格式化信息
		/*
		$user = $this->UserModel->findByPk($order['userId']);
		$order['userName'] = $user['userName'];
		$logistic = $this->LogisticModel->findByPk($order['logisticId']);
		$order['logistic'] = $logistic['title'];
		$order['status'] = $this->get_order_status($order);
		$order['payType'] = Lookup::item('payType', $order['payType']);
		$order['createdTime'] = date('Y-m-d H:i:s', $order['createdTime']);
		*/
		
		if(isset($_POST['consignee'])) {
			$consignee = getgpc('consignee', 'P');
			$this->ConsigneeModel->updateByPk($consigneeId, $consignee);
			$this->message('编辑成功。', GODHOUSE_DOMAIN_WWW.'admin/order-view-'.$order['orderId'].'.htm');
		}
		
		$this->view->assign('order', $order);
		$this->view->assign('consignee', $consignee);
		$this->display('order_edit_consignee');
	}
	
	public function actionStatus() {
		$orderId = getgpc(2);
		
		$order = $this->OrderModel->findByPk($orderId);
		if(!$order) {
			$this->message('错误，订单不存在。', GODHOUSE_DOMAIN_WWW.'admin/order-view-'.$orderId.'.htm');
		}
		$consignee = $this->ConsigneeModel->findByPk($order['consigneeId']);
		if(!$consignee) {
			$this->message('错误，联系方式不存在。', GODHOUSE_DOMAIN_WWW.'admin/order-view-'.$orderId.'.htm');
		}
		
		$comment = getgpc('comment', 'P');
		$orderStatus = getgpc('order', 'P');
		$payStatus = getgpc('pay', 'P');
		$deliveryStatus = getgpc('delivery', 'P');
		
		$operationList = array();
		$att = array();
		if(is_numeric($orderStatus)) {
			$att['orderStatus'] = $orderStatus;
			$operationList[] = Lookup::item('orderStatus', $orderStatus);
		}
		if(is_numeric($payStatus)) {
			$att['payStatus'] = $payStatus;
			$operationList[] = Lookup::item('payStatus', $payStatus);
		}
		if(is_numeric($deliveryStatus)) {
			$att['deliveryStatus'] = $deliveryStatus;
			$operationList[] = Lookup::item('deliveryStatus', $deliveryStatus);
		}
		
		if(!$att) {
			$this->message('请选择要操作的订单状态。', GODHOUSE_DOMAIN_WWW.'admin/order-view-'.$orderId.'.htm');
		}
		$this->OrderModel->updateByPk($orderId, $att);
		
		// 订单状态 
		switch($orderStatus) {
			case GODHOUSE_STATUS_ORDER_COMPLETE:	// 已完成时
				// 更改积分
				$this->UserModel->updateByPk($order['userId'], array(
					'credit'=>'+'.$order['totalPrice'],
					'creditTotal'=>'+'.$order['totalPrice'],
				));
				break;
			case GODHOUSE_STATUS_ORDER_CONFIRMED:	// 确认时
				//$this->ReportBasisModel->inserReportByOrder($order);
				break;
		}
		// 发货状态
		switch ($deliveryStatus) {
			case GODHOUSE_STATUS_DELIVERY_SHIPPED:
				// 发送短信提醒
				$message = "您在波普瑞网站订购的产品已发货，".$comment."";
				$errorNo = $this->sendSMS($message, $consignee['mobile']);
				break;
			case GODHOUSE_STATUS_DELIVERY_PICKING:	// 配货中
				// 将数据插入到报表
				$this->ReportBasisModel->inserReportByOrder($order);
				break;
		}
		
		foreach ($operationList as $v) {
			$operationText = '更改为“'.$v.'”状态';
			$this->OperationModel->insert(array(
				'userId'=>$this->userId,
				'module'=>GODHOUSE_OPERATION_MODULE_ORDER,
				'userName'=>$this->userName,
				'pkId'=>$orderId,
				'content'=>$operationText,
				'comment'=>$comment,
				'createdTime'=>$this->time,
			));
		}
		
		$this->message('操作成功。', GODHOUSE_DOMAIN_WWW.'admin/order-view-'.$orderId.'.htm');
	}
	
	/* 获取订单状态 */
	private function get_order_status($order) {
		$str = Lookup::item('orderStatus', $order['orderStatus']).',';
		if($order['orderStatus'] == GODHOUSE_STATUS_ORDER_CANCEL || $order['orderStatus'] == GODHOUSE_STATUS_ORDER_INVALID || $order['orderStatus'] == GODHOUSE_STATUS_ORDER_REFUSED) {
			$str = '<span style="color:red">'.$str.'</span>';
		}
		$str .= Lookup::item('payStatus', $order['payStatus']).',';
		$str .= Lookup::item('deliveryStatus', $order['deliveryStatus']);
		return $str;
	}
	
	/* 获取操作的button */
	private function get_status_button($order) {
		//echo $order['orderStatus'];
		$buttons = array();

		if($order['orderStatus'] == GODHOUSE_STATUS_ORDER_PENDING) {
			$buttons['order'][GODHOUSE_STATUS_ORDER_CONFIRMED] = '已确认';
		} elseif ($order['orderStatus'] == GODHOUSE_STATUS_ORDER_CONFIRMED) {
			$buttons['order'][GODHOUSE_STATUS_ORDER_COMPLETE] = '已完成';
			$buttons['order'][GODHOUSE_STATUS_ORDER_CANCEL] = '取消';
			$buttons['order'][GODHOUSE_STATUS_ORDER_INVALID] = '无效';
			$buttons['order'][GODHOUSE_STATUS_ORDER_REFUSED] = '退货';
			
			$buttons['pay'][GODHOUSE_STATUS_PAY_UNPAID] = '未付款';
			$buttons['pay'][GODHOUSE_STATUS_PAY_PAID] = '已付款';
			
			$buttons['delivery'][GODHOUSE_STATUS_DELIVERY_PENDING] = '未发货';
			$buttons['delivery'][GODHOUSE_STATUS_DELIVERY_PICKING] = '配货中';
			$buttons['delivery'][GODHOUSE_STATUS_DELIVERY_SHIPPING] = '发货中';
			$buttons['delivery'][GODHOUSE_STATUS_DELIVERY_SHIPPED] = '已发货';
		}
		//print_r($buttons);
		return $buttons;
	}
	
	public function	actionUpdate() {
		$productId = getgpc('productId');
		$updatetype = getgpc('updatetype');

		if($updatetype == 'ajaxupdate') {
			$content = getgpc('remarks', 'P');
			
			$this->ShoppingcartModel->updateByPk($productId,	array('remarks'=>$content));
			exit($content);
		}
		
		
		if($updatetype == 'ajaxdel_date') {
			$content = getgpc('del_date', 'P');
			$this->ShoppingcartModel->updateByPk($productId,	array('del_date'=>$content));
			exit($content);
		}
		
		
		if($updatetype == 'ajaxarr_date') {
			$content = getgpc('arr_date', 'P');
			$this->ShoppingcartModel->updateByPk($productId,	array('arr_date'=>$content));
			exit($content);
		}
		
		
		$this->display('order_view');
	}
	
	
	
	
	public function actionExport() {
		$orderno = getgpc('orderno','R');
		$provinces   = "序号";
		$citys   = "订货号";
		$schools   = "产品说明";
		$assoc   = "市场价格";
		$names   = "会员价格";
		$wangdian   = "数量";
		$duty   = "小计";
		$str .= "<table width=\"800\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
		$str .= "<tr height=40>";  
		$str .= "<td colspan=7 align=center>网上订单</td>";  
		$str .= "</tr>";  
		$str .= "<tr>";
		$str .= "<td height=40 width=50 align=center>$provinces</td>"; 
		$str .= "<td align=center width=100>$citys</td>"; 
		$str .= "<td align=center width=200>$schools</td>"; 
		$str .= "<td align=center width=70>$assoc</td>"; 
		$str .= "<td align=center width=70>$names</td>"; 
		$str .= "<td align=center width=50>$wangdian</td>"; 
		$str .= "<td align=center width=70>$duty</td>"; 
		$str .= "</tr>"; 
			$k=0;
			// 读取购物车信息
			$orderId = getgpc(2);
			$order = $this->OrderModel->findByPk($orderId);
			$shoppingcartList = $this->ShoppingcartModel->findAll("orderId='{$order['orderId']}'");
			//print_r($shoppingcartList);
			$totalPrice = 0;	// 产品价格总计
			$sql = "SELECT * FROM god_shoppingcart where orderId='{$order['orderId']}'";
			$query = $this->db->query($sql);
			
			while ($rs = $this->db->fetch_array($query)) {	
			$userPrice = number_format($rs['ourPrice']*$rs['discount'], 2, '.', '');//会员价格
			$productId = $rs['productId'];
			$ourPrice = $rs['ourPrice'];
			$cartCount = $rs['cartCount'];
			$totalPrice = number_format($userPrice*$rs['cartCount'], 2, '.', '');//小计		
			$k++;
			
			$productNo = $this->ProductModel->findByPk($rs['productId']);
			$dingdanhao = $productNo['productNo'];
			$content = $productNo['content'];
			$totalPrices += $totalPrice;//会员商品总价
		$str .= "<tr height=25>";
		$str .= "<td>$k</td>"; 
		$str .= "<td>$dingdanhao</td>"; 
		$str .= "<td>$content</td>"; 
		$str .= "<td align=right>$ourPrice</td>"; 
		$str .= "<td align=right>$userPrice</td>"; 
		$str .= "<td align=center>$cartCount</td>"; 
		$str .= "<td>￥$totalPrice</td>"; 
		$str .= "</tr>";  
			}
		$str .= "<tr height=25 >";  
		$str .= "<td colspan=7 align=center >产品金额总计：￥$totalPrices&nbsp;&nbsp;实际支付金额：￥{$order[totalPrice]}&nbsp;</td>";  
		$str .= "</tr>";  	
		$str .= "</table>"; 
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=$orderno.xls");
		echo $str;		
			}
			
			
			
//-------------------------------------------企业订单模块------------------------------------------------			
	public function actionQiye() {
		$page = max(1, getgpc('page'));
		$submitTo = getgpc('submitTo', 'R');
		$toUrl = 'order-qiye.htm?';
		$sqladd = '';
		
		$orderNo = getgpc('orderNo', 'R');
		if($orderNo) {
			$sqladd .= " pact like '%$orderNo%' AND";
			$toUrl .= "pact=$orderNo&";
		}
		
		$userName = getgpc('userName', 'R');
		if($userName) {
			$sqladd .= " username like '%$userName%' AND";
			$toUrl .= "username=$userName&";
		}
		
		/* 内部条件 */
		if($this->groupId !=  GODHOUSE_ADMIN_SYSTEM && $this->groupId !=  GODHOUSE_ADMIN_ADVANCED && $this->groupId !=  GODHOUSE_ADMIN_NORMAL) {
			$sqladd .= " adminId='{$this->userId}' AND";
		}
		
		$sqladd = substr($sqladd, 0, -3);
		$toUrl = substr($toUrl, 0, -1);
		$orders = $this->QiyeModel->findAll($sqladd, $page, GODHOUSE_PPP3);
	
		foreach($orders as &$v) {
			
			
			//到货日期提醒
			$v['arr_date'] = $this->QiyeinfoModel->count("qiyeid='$v[id]' AND arr_date = '' ");
			//发货日期提醒
			$v['del_date'] = $this->QiyeinfoModel->count("qiyeid='$v[id]' AND del_date = '' ");

		}
		
		
		$num = $this->QiyeModel->count($sqladd);
		$multipage = page($num, GODHOUSE_PPP3, $page, $toUrl);
		
		$this->view->assign('orders', $orders);
		$this->view->assign('multipage', $multipage);
		$this->display('qiye_admin');
	}
	
	public function actionQiyeimport() {
		$id =	getgpc('id', 'R');
		$username =	getgpc('username', 'R');
		$this->view->assign('id', $id);
		$this->view->assign('username', $username);
		$this->display('qiye_import');
	}
	
	
	public function actionAddimport() {
	
		if(isset($_FILES['userFile'])) {
		
			$userFile = $_FILES['userFile'];
			require_once GODHOUSE_ROOT.'/ExcelReader/reader.php';
			$objWorksheet = new Spreadsheet_Excel_Reader();
			$objWorksheet->setOutputEncoding('utf-8');
			$objWorksheet->read($userFile['tmp_name']);
			
			//print_r($objWorksheet->sheets[0]);exit;
			$rowLength = $objWorksheet->sheets[0]['numRows'];
			$colLength = $objWorksheet->sheets[0]['numCols'];
			$columnIndex = $colLength;

			$id =	getgpc('id', 'R');
			$username =	getgpc('username', 'R');
			$nows = date('Y-m-d H:i:s');
			//print_r($objWorksheet->sheets[0]['cells']);
			for($i=2; $i<=$rowLength; $i++) {
				//for($ii=0; ;$ii<=$colLength; $ii++) {
					$fileds = array(
						
						'qiyeid'=>$id,	
						'xuhao'=>$objWorksheet->sheets[0]['cells'][$i][1],
						'orderno'=>$objWorksheet->sheets[0]['cells'][$i][2],
						'models'=>$objWorksheet->sheets[0]['cells'][$i][3],		
						'amount'=>$objWorksheet->sheets[0]['cells'][$i][4],	
						'price'=>$objWorksheet->sheets[0]['cells'][$i][5],		
						'total'=>$objWorksheet->sheets[0]['cells'][$i][6],		
						'username'=>$username,
						'times'=>$nows,  		//内容
					);

					$this->QiyeinfoModel->insert($fileds);
			}
			$this->message('添加成功。', 'order-qiye.htm');
		} 
		$this->view->assign('message', $message);
		$this->display('imports');
	}
	
	
	public function actionAddorder() {
			
			if($qiye = getgpc('qiye', 'P')) {
			if(!$qiye['username']) {
				$this->message('用户名称不能为空。', 'order-addorder.htm');
			}
			$qiye['names'] = $this->userName;
			$nows = date('Y-m-d H:i:s');
			$qiye['times'] = $nows;			
			$this->QiyeModel->insert($qiye);
			$this->message('添加成功。', 'order-qiye.htm');
		}
			$orderNo = 'BRT_'.date('YmdHi');
			$this->view->assign('orderNo', $orderNo);
			$this->display('order_add');
	}

	public function actionQiyedelete() {
		$id = getgpc(2);
		$this->db->result_first("DELETE FROM god_qiye where id='$id' ");
		$this->db->result_first("DELETE FROM god_qiye_info where qiyeid='$id' ");
		$this->message('删除成功。', GODHOUSE_DOMAIN_WWW.'admin/order-qiye.htm');
	}
			
	public function actionQiyeview() {
		$id = getgpc(2);
		$order = $this->QiyeinfoModel->findAll(" qiyeid=$id ",1, 350);
		
		$mount =  $this->db->result_first("SELECT SUM(amount) as mount FROM god_qiye_info WHERE qiyeid=$id   ");
		$totals =  $this->db->result_first("SELECT SUM(total) as totals FROM god_qiye_info WHERE qiyeid=$id   ");
		$this->view->assign('order', $order);
		$this->view->assign('mount', $mount);
		$this->view->assign('totals', $totals);
		$this->display('order_qiyeview');
	}
	
	//-----------------------------------------------------------------------AJAX修改企业订单
	public function	actionOrderupdate() {
		$id = getgpc('id');
		$updatetype = getgpc('updatetype');
		
		//MYSEE状态 ajax
		if($updatetype == 'ajaxupdate') {
			$bill_date = getgpc('bill_date', 'R');
			$this->QiyeModel->updateByPk($id,	array('bill_date'=>$bill_date));
			exit($bill_date);
		}
		
		
		//货期ajax
		if($updatetype == 'ajaxdeliver') {
			$deliver = getgpc('deliver', 'R');
			$this->QiyeModel->updateByPk($id,	array('deliver'=>$deliver));
			exit($deliver);
		}
		
		//付款方式ajax
		if($updatetype == 'ajaxpayment') {
			$payment = getgpc('payment', 'R');
			$this->QiyeModel->updateByPk($id,	array('payment'=>$payment));
			exit($payment);
		}
		
		//合同额ajax
		if($updatetype == 'ajaxtotal') {
			$total = getgpc('total', 'R');
			$this->QiyeModel->updateByPk($id,	array('total'=>$total));
			exit($total);
		}
		
		
		//项目名称ajax
		if($updatetype == 'ajaxproject') {
			$project = getgpc('project', 'R');
			$this->QiyeModel->updateByPk($id,	array('project'=>$project));
			exit($project);
		}
		
		
		$this->display('qiye_admin');
	}
	
			
	//-----------------------------------------------------------------------AJAX修改企业订单详细内容
	public function	actionOrderview() {
		$id = getgpc('id');
		$updatetype = getgpc('updatetype');
		if($updatetype == 'ajaxupdate') {
			$content = getgpc('content', 'P');
			
			$this->QiyeinfoModel->updateBy($id,	array('content'=>$content));
			exit($content);
		}
		
		
		if($updatetype == 'ajaxdel_date') {
			$content = getgpc('del_date', 'P');
			$this->QiyeinfoModel->updateBy($id,	array('del_date'=>$content));
			exit($content);
		}
		
		
		if($updatetype == 'ajaxarr_date') {
			$content = getgpc('arr_date', 'P');
			$this->QiyeinfoModel->updateBy($id,	array('arr_date'=>$content));
			exit($content);
		}
		
		
		$this->display('order_qiyeview');
	}	
	
	
	
	//--------------------------------------------------------------输出合同范本
	public function actionQiyeexport() {
$id = getgpc('id','R');
$rss = $this->QiyeModel->findByPk($id);
$pact = $rss['pact'];
$times = $rss['times'];
$str .= "<table width=620 cellpadding=0 cellspacing=0 style=font-size:10pt>";
$str .= "<tr height=53>";
$str .= "<td height=60 colspan=6 align=center><font style=font-size:18px; font-weight:bold><strong>产品销售合同</strong></font></td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=35 colspan=3  style=\"border:1px solid #000000\"><strong>合同编号：$pact</strong></td>";
$str .= "<td colspan=3 style=\"border:1px solid #000000\"><strong>项目名称：</strong></td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=35 colspan=3 style=\"border:1px solid #000000\"><strong>签订地点：</strong></td>";
$str .= "<td colspan=3 style=\"border:1px solid #000000\"><strong>甲方：</strong></td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=35 colspan=3 style=\"border:1px solid #000000\"><strong>签订时间：$times</strong></td>";
$str .= "<td colspan=3 style=\"border:1px solid #000000\"><strong>乙方：波普瑞（北京）科技有限公司</strong></td>";
$str .= "</tr>";

$str .= "<tr height=18>";
$str .= "<td height=50 colspan=6 align=left>货币单位:元(RMB/人民币)    </td>";
$str .= "</tr>";
$str .= "<tr height=18>";
$str .= "<td width=57 height=35 align=center style=\"border:1px solid #000000\">序号</td>";
$str .= "<td width=136 height=18 align=center style=\"border:1px solid #000000\">型号单位</td>";
$str .= "";
$str .= "";
$str .= "<td width=69 height=18 align=center style=\"border:1px solid #000000\">数量</td>";
$str .= "<td width=87 height=18 align=center style=\"border:1px solid #000000\">单 价</td>";
$str .= "<td width=100 height=18 align=center style=\"border:1px solid #000000\">合    计</td>";
$str .= "<td width=169 align=center style=\"border:1px solid #000000\">备注</td>";
$str .= "</tr>";

$sql = "SELECT * FROM god_qiye_info where qiyeid='$id' ORDER BY id ASC";
$query = $this->db->query($sql);
$counts=$this->QiyeinfoModel->count("qiyeid='$id'");
$mount =  $this->db->result_first("SELECT SUM(amount) as mount FROM god_qiye_info WHERE qiyeid=$id   ");
$totals =  $this->db->result_first("SELECT SUM(total) as totals FROM god_qiye_info WHERE qiyeid=$id   ");

while ($rs = $this->db->fetch_array($query)) {
if ($counts <='6'){
$xuhao = $rs['xuhao'];
$orderno = $rs['orderno'];
$amount = $rs['amount'];
$price = $rs['price'];
$total = $rs['total'];
$str .= "<tr height=18>";
$str .= "<td height=30 align=center style=\"border:1px solid #000000\">$xuhao</td>";
$str .= "<td height=5 align=center style=\"border:1px solid #000000\">$orderno</td>";
$str .= "<td align=center style=\"border:1px solid #000000\">$amount</td>";
$str .= "<td align=right style=\"border:1px solid #000000\">$price</td>";
$str .= "<td align=right style=\"border:1px solid #000000\">$total</td>";
$str .= "<td align=right style=\"border:1px solid #000000\">&nbsp;</td>";
$str .= "</tr>";

}else{
$str .= "<tr height=18>";
$str .= "<td height=30 align=center style=\"border:1px solid #000000\"></td>";
$str .= "<td height=5 align=center style=\"border:1px solid #000000\">详见清单</td>";
$str .= "<td align=center style=\"border:1px solid #000000\"></td>";
$str .= "<td align=right style=\"border:1px solid #000000\"></td>";
$str .= "<td align=right style=\"border:1px solid #000000\"></td>";
$str .= "<td align=right style=\"border:1px solid #000000\"></td>";
$str .= "</tr>";

break;
}

}
$str .= "<tr height=25>";
$str .= "<td height=25 align=center bordercolor=#000000>&nbsp;</td>";
$str .= "<td align=center bordercolor=#000000>总数量：</td>";
$str .= "<td align=right bordercolor=#000000>$mount</td>";
$str .= "<td align=right bordercolor=#000000>总计：</td>";
$str .= "<td align=right bordercolor=#000000>￥$totals</td>";
$str .= "<td align=right bordercolor=#000000>&nbsp;</td>";
$str .= "<td align=right bordercolor=#000000>&nbsp;</td>";
$str .= "<td align=right bordercolor=#000000>&nbsp;</td>";
$str .= "</tr>";

$str .= "<tr height=42>";
$str .= "<td height=42 colspan=6>Ⅰ质量要求及技术标准：符合相关产品标准；具体包装要求、参数规格以产品样本为准。</td>";
$str .= "</tr>";
$str .= "<tr height=42>";
$str .= "<td height=42 colspan=6>Ⅱ付款方式：甲方预付全款的30%作为定金，乙方备货；甲方付清全款，乙方发货。</td>";
$str .= "</tr>";
$str .= "<tr height=42>";
$str .= "<td height=42 colspan=6>Ⅲ交货地点、方式及费用承担：交货地点为甲方公司，费用由乙方负担。</td>";
$str .= "</tr>";
$str .= "<tr height=42>";
$str .= "<td height=42 colspan=6>Ⅳ质保期：自发货之日起，质量保证期为一年。</td>";
$str .= "</tr>";
$str .= "<tr height=42>";
$str .= "<td height=42 colspan=6>Ⅴ违约责任：甲方如单方面解除合同，甲方需付给乙方合同总额的30%作为违约金。</td>";
$str .= "</tr>";
$str .= "<tr height=42>";
$str .= "<td height=42 colspan=6>Ⅵ解决合同纠纷的方式：双方协商解决，如有异议，由乙方所在地法院裁决。</td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=40 colspan=2 align=center><strong><font style=font-size:16px; font-weight:bold=font-weight:bold>甲     方</font></strong></td>";
$str .= "<td colspan=4 align=center><strong><font style=font-size:16px; font-weight:bold=font-weight:bold>乙     方</font></strong></td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=30 colspan=2>单位（盖章）:</td>";
$str .= "<td colspan=4>单位（盖章）:</td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=30 colspan=2>地址：</td>";
$str .= "<td colspan=4>地址：北京市朝阳区望京东路8号锐创国际中心二号楼510室</td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=30 colspan=2>邮编:</td>";
$str .= "<td colspan=4>邮编:100102</td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=30 colspan=2>签约代表:</td>";
$str .= "<td colspan=4>签约代表:</td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=30 colspan=2>电话:</td>";
$str .= "<td colspan=4>电话:010-84730103</td>";
$str .= "</tr>";
$str .= "<tr height=30>";
$str .= "<td height=30 colspan=2>传真:</td>";
$str .= "<td colspan=4>传真:010-84730132</td>";
$str .= "</tr>";
$str .= "</table>";
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=$pact.xls");
		echo $str;		
			}
			
			
			
	//------------------------------------------------------------输出企业合同清单
	
	public function actionQiyeqingdan() {
		
		$provinces   = "序号";
		$citys   = "订货号";
		$schools   = "产品说明";
		$assoc   = "数量";
		$names   = "单价";
		$wangdian   = "总价";
		$dingd   = "产品清单";
		$pact = getgpc('pact','R');
		
		$str .= "<table width=\"620\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
		$str .= "<tr height=40>";  
		$str .= "<td colspan=6 align=center style=\"font-size:16px;\"><b>$dingd</b></td>";  
		$str .= "</tr>";  
		$str .= "<tr>";
		$str .= "<td height=40 width=66 align=center>$provinces</td>"; 
		$str .= "<td align=center width=100>$citys</td>"; 
		$str .= "<td align=center width=218>$schools</td>"; 
		$str .= "<td align=center width=52>$assoc</td>"; 
		$str .= "<td align=center width=67>$names</td>"; 
		$str .= "<td align=center width=68>$wangdian</td>"; 
		$str .= "</tr>"; 
			$k=0;
			// 读取购物车信息
			$id = getgpc('id','R');
			$sql = "SELECT * FROM god_qiye_info where qiyeid='$id'";
			$query = $this->db->query($sql);
			
			$mount =  $this->db->result_first("SELECT SUM(amount) as mount FROM god_qiye_info WHERE qiyeid=$id   ");
			$totals =  $this->db->result_first("SELECT SUM(total) as totals FROM god_qiye_info WHERE qiyeid=$id   ");
			
			while ($rs = $this->db->fetch_array($query)) {	
			$xuhao = $rs['xuhao'];
			$dingdanhao = $rs['orderno'];
			$models = $rs['models'];
			$price = $rs['price'];
			$productId = $rs['orderno'];
			$total = $rs['total'];
			$amount = $rs['amount'];
			
		$str .= "<tr height=30>";
		$str .= "<td align=center>$xuhao</td>"; 
		$str .= "<td align=\"left\">$dingdanhao</td>"; 
		$str .= "<td>$models</td>"; 
		$str .= "<td align=center>$amount</td>"; 
		$str .= "<td align=right>$price</td>"; 
		$str .= "<td align=center>$total</td>"; 
		$str .= "</tr>";  
			}
		$str .= "<tr height=25 >";  
		$str .= "<td colspan=6 align=center >总数量：$mount&nbsp;&nbsp;实际支付金额：￥$totals&nbsp;</td>";  
		$str .= "</tr>";  	
		$str .= "</table>"; 
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=$pact.xls");
		echo $str;		
			}
			
		//--------------------------批量删除合同清单
		public function	actionAlldelete() {
		$productId = getgpc('productId','P');
		$productId = implode(",",$productId);
		$rs=$this->QiyeinfoModel->delete("id in (".$productId.") ");
		$this->redirect('order-qiye.htm');
	}
	
		//-----------------------解决6个人的工作量做的功能，批量更新网站价格
		/*
		$orderno = $_REQUEST['productId'];
		$id = implode(",",$orderno);
		echo $id;
		$k=0;
		$sql = "SELECT * FROM god_qiye_info where orderno in  ($id)";
		echo $sql;
		$query = $this->db->query($sql);
		while($rs = $this->db->fetch_array($query)) {
			$sqls = "UPDATE god_product SET ourPrice = '".$rs['total']."' WHERE productNo in  ('".$rs['orderno']."')";
			$querys = $this->db->query($sqls);
		}
		
		echo '转换完成';
		*/
		
		
		public function actionZuiprice() {
		$id =	getgpc('id', 'R');
		$username =	getgpc('username', 'R');
		$this->view->assign('id', $id);
		$this->view->assign('username', $username);
		$this->display('z_import');
	}
	
	
	public function actionZuiimport() {
	
		if(isset($_FILES['userFile'])) {
		
			$userFile = $_FILES['userFile'];
			require_once GODHOUSE_ROOT.'/ExcelReader/reader.php';
			$objWorksheet = new Spreadsheet_Excel_Reader();
			$objWorksheet->setOutputEncoding('utf-8');
			$objWorksheet->read($userFile['tmp_name']);
			
			//print_r($objWorksheet->sheets[0]);exit;
			$rowLength = $objWorksheet->sheets[0]['numRows'];
			$colLength = $objWorksheet->sheets[0]['numCols'];
			$columnIndex = $colLength;

			$id =	getgpc('id', 'R');
			$username =	getgpc('username', 'R');
			$nows = date('Y-m-d H:i:s');
			for($i=2; $i<=$rowLength; $i++) {
					$fileds = array(
						
						
						'xuhao'=>$objWorksheet->sheets[0]['cells'][$i][1],
						'price'=>$objWorksheet->sheets[0]['cells'][$i][2],
						
					);

					
					
			$xuhao = $objWorksheet->sheets[0]['cells'][$i][1];
			$price = $objWorksheet->sheets[0]['cells'][$i][2];
			$fenlei1 = $objWorksheet->sheets[0]['cells'][$i][3];
			$fenlei2 = $objWorksheet->sheets[0]['cells'][$i][4];
			$fenlei3 = $objWorksheet->sheets[0]['cells'][$i][5];
			
			
			$sql = "UPDATE god_product SET ourPrice = '$price' WHERE productNo = '$xuhao'";
			$query = $this->db->query($sql);
			
			if ($xuhao!=''){
			$sqls = "SELECT * FROM  god_product WHERE productNo = '".trim($xuhao)."'";
			$rs = $this->db->result_first($sqls);
			if ($rs==''){
				$sql2 = "INSERT INTO  god_product (categoryId,parentId,upperparentId,productNo,ourPrice) VALUES ('$fenlei1','$fenlei2','$fenlei3','$xuhao','$price') ";
				$querys = $this->db->query($sql2);
			}
			}
	
			}
			$this->message('添加成功。', 'product-admin.htm');
		} 
		$this->view->assign('message', $message);
		$this->display('imports');
	}
}